package com.water.locationservice.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.water.locationservice.entites.LocationInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author 汪胜彬
 * @version 1.0
 */

@Service
@Slf4j
public class LocationService {

    @Value("${baidu.host}")
    private String host;
    @Value("${baidu.method}")
    private String method;
    @Value("${baidu.ak}")
    private String ak;
    private String args;

    @Resource
    RestTemplate restTemplate;

    public String getLocation() {

        args = "ak=" + ak + "&coor=bd09ll";


        String url = host + "/" + method + "?" + args;
        log.info(url);
        JSONObject locationInfoJson = restTemplate.getForObject(url, JSONObject.class);
        String adcodeString = locationInfoJson.getJSONObject("content").getJSONObject("address_detail").getString("adcode");
//        log.info("city: " + city);
//        LocationInfo locationInfo = JSON.parseObject(locationInfoJson, LocationInfo.class);
//        log.info("locationInfoJson: " + locationInfoJson);
        return adcodeString;
    }
}
